package com.xh.one2one.lesson.controller;

import com.xh.bussiness.permission.model.User;
import com.xh.bussiness.permission.service.IUserService;
import com.xh.bussiness.thirdapi.xinzhou.model.XzTeacherInfo;
import com.xh.bussiness.thirdapi.xinzhou.service.IXinzhouTmsService;
import com.xh.core.bean.AjaxResult;
import com.xh.core.bean.XhExportHeaderBean;
import com.xh.core.bean.XhPage;
import com.xh.core.constant.AjaxResultConstant;
import com.xh.core.controller.BaseController;
import com.xh.core.util.MD5Utils;
import com.xh.core.util.StringUtils;
import com.xh.core.util.UserUtils;
import com.xh.one2one.lesson.bean.request.LessonInfoRequest;
import com.xh.one2one.lesson.bean.request.LessonPlanRequest;
import com.xh.one2one.lesson.model.LessonInfo;
import com.xh.one2one.lesson.model.LessonPlan;
import com.xh.one2one.lesson.service.ILessonInfoService;
import com.xh.one2one.lesson.service.ILessonPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 备课计划
 */
@RestController
@RequestMapping("/lessonPlan")
public class LessonPlanController extends BaseController {

    @Autowired
    private ILessonPlanService lessonPlanService;

    /**
     *
     * @param lessonPlan
     * @return
     */
    @PostMapping("/saveOrUpdate")
    public AjaxResult saveOrUpdate(LessonPlan lessonPlan) {
        // 给学生 id 排序， 生成 markid
        if(StringUtils.isNotEmpty(lessonPlan.getStudentIds())){
            if(lessonPlan.getStudentIds().contains(",")){
                String[] ids = lessonPlan.getStudentIds().split(",");
                Arrays.sort(ids);
                StringJoiner sj = new StringJoiner(",");
                for (String id : ids) {
                    sj.add(id);
                }
                lessonPlan.setMarkId(MD5Utils.string2MD5(lessonPlan.getTeacherId()+lessonPlan.getSubjectId()+sj.toString()));
            }else{
                lessonPlan.setMarkId(MD5Utils.string2MD5(lessonPlan.getTeacherId()+lessonPlan.getSubjectId()+lessonPlan.getStudentIds()));
            }
        }
        if(StringUtils.isEmpty(lessonPlan.getId())){
            lessonPlanService.insert(lessonPlan);
        }else{
            lessonPlanService.update(lessonPlan);
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, lessonPlan);
    }

    @PostMapping("/queryByPage")
    public AjaxResult queryByPage(XhPage<LessonPlan> page, LessonPlanRequest lessonPlanRequest) {

        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, lessonPlanService.queryByPage(page, lessonPlanRequest));
    }

    @PostMapping("/delete")
    public AjaxResult delete(String id) {

        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, lessonPlanService.delete(id));
    }


    @PostMapping("/queryStudents")
    public AjaxResult queryStudents() {

        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, lessonPlanService.queryStudents());
    }
}
